package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.WorkflowBusiness;
import com.hunttown.mes.rpc.domain.WorkflowBusinessDTO;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import com.hunttown.mes.rpc.exception.ConvertException;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2019-08-01 15:48:31
 */
public class WorkflowBusinessConverts {

    private static Logger LOG = LoggerFactory.getLogger(WorkflowBusinessConverts.class);

    public static List<WorkflowBusinessDTO> convert(List<WorkflowBusiness> workflowBusinessList) {
        if (CollectionUtils.isEmpty(workflowBusinessList)) {
            return Collections.EMPTY_LIST;
        }

        List<WorkflowBusinessDTO> workflowBusinessListResult = Lists.newArrayList();
        for (WorkflowBusiness workflowBusiness : workflowBusinessList) {
            workflowBusinessListResult.add(convert(workflowBusiness));
        }
        return workflowBusinessListResult;
    }

    public static WorkflowBusinessDTO convert(WorkflowBusiness workflowBusiness) {
        try {
            return BeanUtils.copy(workflowBusiness, WorkflowBusinessDTO.class);
        } catch (Exception e) {
            LOG.error("WorkflowBusiness到WorkflowBusinessDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "WorkflowBusiness到WorkflowBusinessDTO转换失败", e);
        }
    }

    public static WorkflowBusiness convert(WorkflowBusinessDTO workflowBusinessDTO) {
        try {
            return BeanUtils.copy(workflowBusinessDTO, WorkflowBusiness.class);
        } catch (Exception e) {
            LOG.error("WorkflowBusinessDTO到WorkflowBusiness转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "WorkflowBusinessDTO到WorkflowBusiness转换失败", e);
        }
    }
}